package com.ruoyi.ffmpeg;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ws.schild.jave.EncoderException;
import ws.schild.jave.InputFormatException;
import ws.schild.jave.MultimediaObject;
import ws.schild.jave.ScreenExtractor;
import ws.schild.jave.info.MultimediaInfo;
import ws.schild.jave.process.ProcessLocator;
import ws.schild.jave.process.ProcessWrapper;
import ws.schild.jave.process.ffmpeg.DefaultFFMPEGLocator;
import ws.schild.jave.utils.RBufferedReader;
import ws.schild.jave.utils.Utils;

import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;

public class VideoExtractor {

    private static final Logger logger = LoggerFactory.getLogger(VideoExtractor.class);

    private final ProcessLocator locator;

    public VideoExtractor() {
        this.locator = new DefaultFFMPEGLocator();
    }

    public static void main(String[] args) {
        File file = new File("D:\\aaa\\[电影天堂www.dytt89.com]追虎擒龙-2021_HD国语中字.mp4") ;
        File target = new File("D:\\aaa\\[电影天堂www.dytt89.com]追虎擒龙-2021_HD国语中字-3.mp4");
        MultimediaObject multimediaObject = new MultimediaObject(file);
        VideoExtractor videoExtractor = new VideoExtractor();
        videoExtractor.renderVideo(multimediaObject, 150000, 60000, target, true);

    }

    public void renderVideo(
            MultimediaObject multimediaObject,
            long start,
            long end,
            File outputFile,
            boolean keyframesSeeking){
        String inputSource = multimediaObject.toString();
        try {
            if (outputFile.exists()) {
                outputFile.delete();
            }
            if (!multimediaObject.isURL() && !multimediaObject.getFile().canRead()) {
                logger.debug("Failed to open input file");
                throw new SecurityException();
            }
        } catch (SecurityException e) {
            logger.debug("Access denied checking destination folder", e);
        }

        ProcessWrapper ffmpeg = this.locator.createExecutor();
        if (keyframesSeeking) {
            ffmpeg.addArgument("-ss");
            ffmpeg.addArgument(Utils.buildTimeDuration(start));
        }
        ffmpeg.addArgument("-i");
        ffmpeg.addArgument(inputSource);
        if (!keyframesSeeking) {
            ffmpeg.addArgument("-ss");
            ffmpeg.addArgument(Utils.buildTimeDuration(start));
        }
        ffmpeg.addArgument("-t");
        ffmpeg.addArgument(Utils.buildTimeDuration(end));
        ffmpeg.addArgument("-c:v");
        ffmpeg.addArgument("copy");
        ffmpeg.addArgument("-c:a");
        ffmpeg.addArgument("copy");
        ffmpeg.addArgument(outputFile.getAbsolutePath());

        try {
            ffmpeg.execute();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            RBufferedReader reader = new RBufferedReader(new InputStreamReader(ffmpeg.getErrorStream()));
            int lineNR = 0;
            String line;
            while ((line = reader.readLine()) != null) {
                lineNR++;
                logger.debug("Input Line ({}): {}", lineNR, line);
                // TODO: Implement additional input stream parsing
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            ffmpeg.destroy();
        }
    }


}
